from scrapy.spider import BaseSpider
from scrapy.selector import Selector

from spider.items import SpiderItem
import MySQLdb


class SuperSpider(BaseSpider):

    name = "super"
    baseurl = "http://www.interface.autohome.com.cn/dealer/specdealerlist/spec/pricelist/"

    conn = MySQLdb.connect(host="localhost",user="spider",passwd="Xjk@4047218?",db="spider")
    cursor = conn.cursor()
    sql = "select autohomepid,url,maxpages from sp_ah_carmodel where state = '0' order by id asc"
    result = cursor.execute(sql)
    priceurl = []
    for row in cursor.fetchall():
        pid =  row[1].split("/")[-2]
        url =  baseurl+str(row[0])+"-"+str(pid)+"-1-0-0-0-0-1-"
        if row[2] > 1:
            for j in range(1,row[2]+1):
                priceurl.append(url+str(j))
        else:
            priceurl.append(url+"1")

    cursor.close()
    #start_urls = ["http://www.interface.autohome.com.cn/dealer/specdealerlist/spec/pricelist/18-18103-1-0-0-0-0-1-1"]
    #print start_urls
    start_urls = priceurl


    def parse(self,response):
        sel = Selector(response)
        item = SpiderItem()   
        temp = response.url.split("/")[-1].split("-")        

        item["c1"] = temp[0]
        item["c2"] = temp[1]

        area = sel.xpath('//ul[@class="deal-list"]')

        item["carshop"] = area.xpath('.//li//div[@class="deal-list-infor"]//dt[1]//a/text()').extract()
        item["tel"] = area.xpath('.//li//div[@class="deal-list-infor"]//dd[1]//span[2]//span/text()').extract()
        item["province"] = area.xpath('.//li//div[@class="deal-list-city"]//div[1]/text()').extract()
        item["town"] = area.xpath('.//li//div[@class="deal-list-city"]//div[2]/text()').extract()
        item["price"] = area.xpath('.//li//div[@class="deal-list-price"]//div[@class="price-link"]//a[2]//span/text()').extract()
        item["type"] = "price"
        return item

